package com.xindaoapp.happypet.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.SplashPictureBean.SplashBannerInfo;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.share.SharePopupWindow;

/**
 * 查看榜单
 * 
 * @author Yangqing
 * 
 */
public class SeeRankingListActivity extends BaseActivity {

    private ViewPager vViewPager;
    private View[] tabViews;
    private String mTid;
    private SplashBannerInfo shaituinfo;

    @Override
    protected int getContentViewResId() {
        return R.layout.layout_ranking;
    }


    @Override
    protected void initViews() {
        super.initViews();
        mTid = getIntent().getStringExtra(Constants.KEY_TID);
        shaituinfo = (SplashBannerInfo) getIntent().getSerializableExtra(Constants.KEY_SHAITUINFO);

        vViewPager = (ViewPager) findViewById(R.id.mViewPager);

        tabViews = new View[] {findViewById(R.id.zan_ranking), findViewById(R.id.discuss_ranking)};
        CommonRankingAdapter adapter = new CommonRankingAdapter(getSupportFragmentManager());
        vViewPager.setOnPageChangeListener(mChangeListener);
        vViewPager.setAdapter(adapter);
        for (int i = 0; i < tabViews.length; i++) {
            final int temp = i;
            tabViews[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    vViewPager.setCurrentItem(temp);
                }
            });
        }
        vViewPager.setCurrentItem(0);
    }

    public class CommonRankingAdapter extends FragmentPagerAdapter {

        public CommonRankingAdapter(FragmentManager fm) {
            super(fm);
            resetTab(0);
        }

        @Override
        public Fragment getItem(int position) {
            return RankingFragment.newInstance(position, mTid);
        }

        @Override
        public int getCount() {
            return 2;
        }

    }

    private final OnPageChangeListener mChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            resetTab(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {}
    };

    protected void resetTab(int currentPageIndex) {
        if (currentPageIndex == 0) {
            tabViews[0].setSelected(true);
            tabViews[1].setSelected(false);
        } else {
            tabViews[1].setSelected(true);
            tabViews[0].setSelected(false);
        }
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected String getTopBarTitleStrRes() {
        return "活动榜单";
    }

    @Override
    protected int getTopBarRightImageViewRes() {
        return R.drawable.share_default;
    }


    @Override
    protected OnClickListener getRightViewClickListener() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shaituinfo != null) {
                    share();
                }
            }
        };
    }

    private void share() {
        if (UserState.isLogged(SeeRankingListActivity.this)) {
            SharePopupWindow win =
                    new SharePopupWindow(SeeRankingListActivity.this, shaituinfo.coverpath, false);
            win.setDefaultIconId(R.drawable.icon);
            win.setPostCome("shaitu");
            String url =
                    String.format(
                            "http://bbs.chinapet.com/plugin.php?id=leepet_thread:shaiturank&shaituid=%1$s&uid=%2$s",
                            shaituinfo.actid, UserState.getUserUid());

            String contant =
                    String.format(
                            "#尾巴圈宠物# 尾巴圈正在举行%1$s活动,还有更多有趣的活动，快来参加吧!，@尾巴圈，我的养宠神器！下载地址：%2$s（来自@尾巴圈）",
                            shaituinfo.tag, url);

            String WXContant = String.format("尾巴圈正在举行%s活动，快来参加吧", shaituinfo.tag);

            String QQContant = String.format("尾巴圈正在进行%s活动，快来参加吧", shaituinfo.tag);


            win.setInfors(SeeRankingListActivity.this, contant, "尾巴圈，我的养宠神器", contant, WXContant,
                    QQContant, url).showAtLocation(vViewPager, Gravity.BOTTOM, 0, 0);
            // win.setInfors(SplashPictureActivity2.this, contant, contant, contant, url)
            // .showAtLocation(findViewById(R.id.ll_layout), Gravity.BOTTOM, 0, 0);

            CommonUtil.addScreenBg(win, SeeRankingListActivity.this);
        }
    }

}
